package com.formulas.component.dto;

import java.io.Serial;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 响应用批处理页面记录返回，通常用于页面查询
 * @author yiwannuofulasi
 * @date 2023/6/2 11:14
 */
public class PageResponse<T> extends Response {

    @Serial
    private static final long serialVersionUID = 1L;

    /**记录总数**/
    private int totalCount = 0;
    /**分页大小**/
    private int pageSize = 1;
    /**分页下标**/
    private int pageIndex = 1;
    /**数据集合Collection<T>**/
    private Collection<T> data;

    /**
     * 获取记录总数
     * @return int
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * 设置记录总数
     * @param totalCount int
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 获取分页大小
     * @return
     */
    public int getPageSize() {
        if (pageSize < 1) {
            return 1;
        }
        return pageSize;
    }

    /**
     * 设置分页大小
     * @param pageSize int
     */
    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            this.pageSize = 1;
        } else {
            this.pageSize = pageSize;
        }
    }

    /**
     * 获取分页下标
     * @return int
     */
    public int getPageIndex() {
        if (pageIndex < 1) {
            return 1;
        }
        return pageIndex;
    }

    /**
     * 设置分页下标
     * @param pageIndex int
     */
    public void setPageIndex(int pageIndex) {
        if (pageIndex < 1) {
            this.pageIndex = 1;
        } else {
            this.pageIndex = pageIndex;
        }
    }

    /**
     * 获取数据集合List<T>
     * @return List<T>
     */
    public List<T> getData() {
        if (null == data) {
            return Collections.emptyList();
        }
        if (data instanceof List) {
            return (List<T>) data;
        }
        return new ArrayList<>(data);
    }

    /**
     * 设置数据集合Collection<T>
     * @param data Collection<T>
     */
    public void setData(Collection<T> data) {
        this.data = data;
    }

    /**
     * 获取记录总页数
     * @return int
     */
    public int getTotalPages() {
        return this.totalCount % this.pageSize == 0 ? this.totalCount
                / this.pageSize : (this.totalCount / this.pageSize) + 1;
    }

    /**
     * 判断数据集合是否为空
     * @return boolean
     */
    public boolean isEmpty() {
        return data == null || data.isEmpty();
    }

    /**
     * 判断数据集合是否不为空
     * @return boolean
     */
    public boolean isNotEmpty() {
        return !isEmpty();
    }

    /**
     * 成功构建响应
     * @return PageResponse
     */
    public static PageResponse buildSuccess() {
        PageResponse response = new PageResponse();
        response.setSuccess(true);
        return response;
    }

    /**
     * 失败构建响应
     * @param errCode 错误代码
     * @param errMessage 错误消息
     * @return PageResponse
     */
    public static PageResponse buildFailure(String errCode, String errMessage) {
        PageResponse response = new PageResponse();
        response.setSuccess(false);
        response.setErrCode(errCode);
        response.setErrMessage(errMessage);
        return response;
    }

    /**
     * 构建空数据集合响应
     * @param pageSize 分页大小
     * @param pageIndex 分页下标
     * @return PageResponse<T>
     * @param <T> Object
     */
    public static <T> PageResponse<T> of(int pageSize, int pageIndex) {
        PageResponse<T> response = new PageResponse<>();
        response.setSuccess(true);
        response.setData(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(pageSize);
        response.setPageIndex(pageIndex);
        return response;
    }

    /**
     * 构建数据集合响应
     * @param data 数据集合
     * @param totalCount 记录总数
     * @param pageSize 分页大小
     * @param pageIndex 分页下标
     * @return PageResponse<T>
     * @param <T> Object
     */
    public static <T> PageResponse<T> of(Collection<T> data, int totalCount, int pageSize, int pageIndex) {
        PageResponse<T> response = new PageResponse<>();
        response.setSuccess(true);
        response.setData(data);
        response.setTotalCount(totalCount);
        response.setPageSize(pageSize);
        response.setPageIndex(pageIndex);
        return response;
    }
}
